<template>
	<view v-if="open" class="">
		<view class="bg">
			<view class="content">
				<view class="head">
					切换爱宠
					<image @click="handleClose" src="https://qiyeji.oss-cn-beijing.aliyuncs.com/20250111/202412181550404015.png" mode=""></image>
				</view>
				<view class="list">
					<view @click="handleSwitch(item)" v-for="(item,index) in cargoList" class="item" :key="index">
						<view class="item-img">
							<image :src="item.picture" mode="aspectFill"></image>
						</view>
						<view class="item-content text-cut">
							<view class="item-content-head">
								<view class="item-content-name text-cut">{{item.name || '--'}}</view>
								<view class="item-content-icon">
									<image class="item-content-icon" :src="item.sex == 'Male'? man : woman" mode="aspectFill"></image>
								</view>
								<view class="item-content-age">
									{{item.age || '--'}}
								</view>
							</view>
							<view class="item-content-id">
								ID：{{item.cargoNo}}
							</view>
						</view>
						<image class="item-icon" src="https://qiyeji.oss-cn-beijing.aliyuncs.com/20250111/202412181612252718.png" mode=""></image>
					</view>
					<view style="margin-bottom:50rpx;">
						<data-null v-if="!cargoList.length"></data-null>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getCargoList,
	} from "@/api/pet/binding/index"
	export default {
		data(){
			return{
				cargoList:[],
				open:false,
				success:null,
				man:'https://qiyeji.oss-cn-beijing.aliyuncs.com/20250111/202412181602393331.png',
				woman:'https://qiyeji.oss-cn-beijing.aliyuncs.com/20250111/202412181602585604.png',
			}
		},
		
		methods:{
			init(e){
				this.success = e.success?e.success:null
				this.getCargoList()
			},
			getCargoList() {
				getCargoList({
				}).then(res => {
					this.cargoList = res || []
					let pet_cargoId = uni.getStorageSync('pet_cargoId')
					let index = this.cargoList.findIndex(item=>{
						return item.id == pet_cargoId
					})
					let currentIndex = index == -1 ? 0 : index
					// || currentIndex == 0
					console.log(this.cargoList[currentIndex])
					if(this.cargoList[currentIndex]){
						this.handleSwitch(this.cargoList[currentIndex])
					}else{
						uni.removeStorageSync('pet_cargoId')
					}
				})
			},
			handleOpen(e){
				this.open = true
				this.success = e.success?e.success:null
			},
			handleSwitch(item){
				if(this.success){
					uni.setStorageSync('pet_cargoId',item.id)
					this.success(item)
				}
				this.handleClose()
			},
			handleClose(){
				this.open = false
			}
		},
	}
</script>

<style lang="scss" scoped>
	.bg{
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100vh;
		z-index: 9;
		background: rgba(0, 0, 0, .5);
		.content{
			width: 100%;
			
			position: absolute;
			left: 0;
			bottom: 0;
			background: #fff;
			border-radius: 40rpx 40rpx 0rpx 0rpx;
			.head{
				width: 100%;
				padding: 32rpx 0 64rpx;
				position: relative;
				font-family: PingFangSC, PingFang SC;
				font-weight: 500;
				font-size: 30rpx;
				color: var(--h1Color);
				line-height: 42rpx;
				text-align: center;
				image{
					width: 40rpx;
					height: 40rpx;
					position: absolute;
					top: 32rpx;
					right: 32rpx;
				}
			}
			.list{
				width: 100%;
				max-height: 70vh;
				padding: 0 24rpx 92rpx;
				box-sizing: border-box;
				overflow: auto;
				// display: flex;
				// flex-direction: column;
				.item{
					width: 100%;
					height: 200rpx;
					background: #FFFFFF;
					border-radius: 16rpx;
					padding: 0 32rpx;
					box-sizing: border-box;
					display: flex;
					align-items: center;
					margin-bottom: 24rpx;
					&:last-child{
						margin-bottom: 0;
					}
					.item-img{
						width: 120rpx;
						height: 120rpx;
						margin-right: 24rpx;
						border-radius: 50%;
						overflow: hidden;
						background: #eee;
						image{
							width: 100%;
							height: 100%;
						}
					}
					.item-content{
						flex: 1;
						.item-content-head{
							width: 100%;
							display: flex;
							align-items: center;
							margin-bottom: 8rpx;
							.item-content-name{
								font-family: PingFangSC, PingFang SC;
								font-weight: 600;
								font-size: 36rpx;
								color: var(--h1Color);
								line-height: 50rpx;
								margin-right: 8rpx;
							}
							.item-content-icon{
								width: 36rpx;
								height: 36rpx;
							}
							.item-content-age{
								font-family: PingFangSC, PingFang SC;
								font-weight: 400;
								font-size: 24rpx;
								color: var(--h2Color);
								line-height: 1;
								padding-left: 16rpx;
								border-left: 1rpx solid #DDDDDD;
								margin-left: 16rpx;
							}
						}
						.item-content-id{
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							font-size: 24rpx;
							color: var(--h1Color);
							line-height: 40rpx;
							width: max-content;
							padding: 0 16rpx;
							background: #F8F8F8;
							border-radius: 21rpx;
						}
					}
					.item-icon{
						width: 30rpx;
						height: 30rpx;
						margin-left: 10rpx;
					}
				}
			}
		}
	}
</style>